<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{

    public function _initialize()
    {
        if (empty($_SESSION['empid'])) {
            
            // $this->error('页面加载中。。。',U('Login/Login'),1);
            $this->redirect('Login/Login');
        }
    }

    public function Index()
    {
        if (IS_POST) {
            $star_time = I('post.start_time');
            $end_time = I('post.end_time');
            
            $d = strtotime($star_time);
            $en = strtotime($end_time);
            if ($d > $en or empty($d) or empty($en)) {
                
                $this->error('时间区间选择有误');
            } else {
                $ts = (($en - $d) / 86400) + 1;
                for ($i = 0; $i < $ts; $i ++) {
                    
                    $riqi = date('Y-m-d', $d);
                    $star_time = date('Y-m-d', $d);
                    $end_time = date('Y-m-d', $d);
                    $map['start_time'] = array(
                        'between',
                        '' . $star_time . ',' . $end_time . ''
                    );
                    
                    $sq[$riqi] = M('CCourse')->where($map)->select();
                    foreach ($sq[$riqi] as $k => $v) {
                        
                        $map['scheid'] = $v['courseid'];
                        $sj = M('CSchedule')->where($map)
                            ->field('schename,dteacher,jteacher')
                            ->find();
                        $sq[$riqi][$k]['schename'] = $sj['schename'];
                        $sq[$riqi][$k]['dteacher'] = $sj['dteacher'];
                        $sq[$riqi][$k]['jteacher'] = $sj['jteacher'];
                    }
                    
                    $d += 86400;
                }
            }
        } else {
            
            if (I('get.sd') != '') {
                if (I('get.fs') == 'down') {
                    $sd1 = I('get.sd');
                    $sd = $sd1 + (7 * 24 * 60 * 60);
                } elseif (I('get.fs') == 'up') {
                    $sd1 = I('get.sd');
                    $sd = $sd1 - (7 * 24 * 60 * 60);
                } elseif (I('get.fs') == 'now') {
                    $week = date('w');
                    if ($week == 1) {
                        $sd = strtotime(date('Y-m-d'));
                    } else {
                        $sd = strtotime('last monday');
                    }
                }
            } else {
                $week = date('w');
                if ($week == 1) {
                    $sd = strtotime(date('Y-m-d'));
                } else {
                    $sd = strtotime('last monday');
                }
            }
            
            $this->assign('sd', $sd);
            $d = $sd;
            
            for ($i = 0; $i < 7; $i ++) {
                
                $riqi = date('Y-m-d', $d);
                $star_time = date('Y-m-d', $d);
                $end_time = date('Y-m-d', $d);
                $map['start_time'] = array(
                    'between',
                    '' . $star_time . ',' . $end_time . ''
                );
                
                $sq[$riqi] = M('CCourse')->where($map)->select();
                foreach ($sq[$riqi] as $k => $v) {
                    
                    $map['scheid'] = $v['courseid'];
                    $sj = M('CSchedule')->where($map)
                        ->field('schename,dteacher,jteacher')
                        ->find();
                    $sq[$riqi][$k]['schename'] = $sj['schename'];
                    $sq[$riqi][$k]['dteacher'] = $sj['dteacher'];
                    $sq[$riqi][$k]['jteacher'] = $sj['jteacher'];
                }
                
                $d += 86400;
            }
        }
        $class = R('Pub/Classs');
        $teacher = R('Pub/Teacher');
        $this->assign('teacher', $teacher);
        
        $this->assign('class', $class);
        // var_dump($sq);
        $this->assign('sq', $sq);
        $this->assign('riqi', $riqi);
        $this->display('index');
    }

    public function index2()
    {
        if (I('get.fs') == 'down') {
            $stime = I('get.sd') + (7 * 24 * 60 * 60);
        } elseif (I('get.fs') == 'up') {
            $stime = I('get.sd') - (7 * 24 * 60 * 60);
        } elseif (I('get.fs') == 'now') {
            $week = date('w');
            
            if ($week == 1) {
                
                $stime = strtotime(date('Y-m-d'));
            } else {
                $stime = strtotime('last monday');
            }
        }
        
        $etime = $stime + (6 * 24 * 60 * 60);
        
        $stime2 = date('Y-m-d', $stime);
        $etime2 = date('Y-m-d', $etime);
        echo $stime2;
        echo $etime2;
        
        $where['start_time'] = array(
            'between',
            '' . $stime2 . ',' . $etime2 . ''
        );
        
        $ba = M('c_course')->where($where)
            ->Distinct(true)
            ->field('courseid')
            ->select();
        foreach ($ba as $k => $v) {
            
            $mag['scheid'] = $v['courseid'];
            $sq[0][$k] = M('c_schedule')->where($mag)
                ->field('schename,scheid')
                ->select();
        }
        
        if (IS_POST) {
            $star_time = I('post.start_time');
            $end_time = I('post.end_time');
            
            $d = strtotime($star_time);
            $en = strtotime($end_time);
            if ($d > $en or empty($d) or empty($en)) {
                
                $this->error('时间区间选择有误');
            } else {
                $ts = (($en - $d) / 86400) + 1;
                for ($i = 0; $i < $ts; $i ++) {
                    
                    $riqi = date('Y-m-d', $d);
                    $star_time = date('Y-m-d', $d);
                    $end_time = date('Y-m-d', $d);
                    $map['start_time'] = array(
                        'between',
                        '' . $star_time . ',' . $end_time . ''
                    );
                    
                    $sq[1][$riqi] = M('c_course')->where($map)->select();
                    
                    foreach ($sq[1][$riqi] as $k => $v) {
                        
                        $map['scheid'] = $v['courseid'];
                        $sj = M('c_schedule')->where($map)->getField('schename');
                        $sq[1][$riqi][$k]['schename'] = $sj;
                    }
                    
                    $d += 86400;
                }
            }
        } else {
            
            if (I('get.sd') != '') {
                if (I('get.fs') == 'down') {
                    $sd1 = I('get.sd');
                    $sd = $sd1 + (7 * 24 * 60 * 60);
                } elseif (I('get.fs') == 'up') {
                    $sd1 = I('get.sd');
                    $sd = $sd1 - (7 * 24 * 60 * 60);
                } elseif (I('get.fs') == 'now') {
                    $week = date('w');
                    if ($week == 1) {
                        $sd = strtotime(date('Y-m-d'));
                    } else {
                        $sd = strtotime('last monday');
                    }
                }
            } else {
                $week = date('w');
                if ($week == 1) {
                    $sd = strtotime(date('Y-m-d'));
                } else {
                    $sd = strtotime('last monday');
                }
            }
            $this->assign('sd', $sd);
            $d = $sd;
            
            for ($i = 0; $i < 7; $i ++) {
                
                $riqi = date('Y-m-d', $d);
                $star_time = date('Y-m-d', $d);
                $end_time = date('Y-m-d', $d);
                $map['start_time'] = array(
                    'between',
                    '' . $star_time . ',' . $end_time . ''
                );
                
                $sq[1][$riqi] = M('CCourse')->where($map)->select();
                foreach ($sq[1][$riqi] as $k => $v) {
                    
                    $map['scheid'] = $v['courseid'];
                    $sj = M('CSchedule')->where($map)->getField('schename');
                    $sq[1][$riqi][$k]['schename'] = $sj;
                }
                
                $d += 86400;
            }
        }
        
        $class = R('Pub/Classs');
        $this->assign('class', $class);
        var_dump($sq);
        $this->assign('sq', $sq);
        $this->assign('riqi', $riqi);
        
        $this->display();
    }

    public function header()
    {
        $user = M('sys_user');
        $map['empid'] = session('empid');
        $usinfo = $user->where($map)->find();
        
        return $usinfo;
        $this->display();
    }

    public function left()
    {
        $this->display();
    }

    public function kcshow()
    {
        if (IS_AJAX) {
            $map['coursename'] = I('post.kcname');
            $map['courseid'] = I('post.bh');
            $kc = M('CCourse')->where($map)->find();
            $this->assign('kc', $kc);
            $this->display();
        }
    }

    public function bcshow()
    {
        if (IS_AJAX) {
            $map['schename'] = I('post.bcmc');
            $scheid = M('CSchedule')->where($map)->find();
            $where['classid'] = $scheid['scheid'];
            $student = M('CStudent')->where($where)->select();
            $mas['courseid'] = $scheid['scheid'];
            $kc = M('CCourse')->where($mas)->select();
            $this->assign('kecheng', $kc);
            $this->assign('sche', $scheid);
            $this->assign('student', $student);
            // var_dump($student);
            $this->display();
        }
    }

    public function xgmm()
    {
        if (IS_POST) {
            $opassword = MD5($_POST['opassword']);
            $npassword = MD5(I('post.password'));
            $map['loginname'] = I('post.loginname');
            $password = M('SysUser')->where($map)->getField('password');
            if ($opassword == $password) {
                $data['password'] = $npassword;
                $data['zt'] = 1;
                $up = M('SysUser')->where($map)->save($data);
                if ($up) {
                    $data = 0;
                } else {
                    
                    $data = 1;
                }
            } else {
                $data = 3;
            }
            
            echo $data;
        }
    }

    public function saveclass()
    {
        $map['id'] = I('post.id');
        $data['class'] = I('post.value');
        $up = M('CCourse')->where($map)->save($data);
        if ($up) {
            $data = 0;
        } else {
            $data = 1;
        }
        echo $data;
    }

    public function savedteacher()
    {
        $map['scheid'] = I('post.id');
        $data['dteacher'] = I('post.value');
        $up = M('CSchedule')->where($map)->save($data);
        if ($up) {
            $data = 0;
        } else {
            $data = 1;
        }
        echo $data;
    }

    public function savejteacher()
    {
        $map['scheid'] = I('post.id');
        $data['jteacher'] = I('post.value');
        $up = M('CSchedule')->where($map)->save($data);
        if ($up) {
            $data = 0;
        } else {
            $data = 1;
        }
        echo $data;
    }
}